/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.st.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jeesite.modules.st.entity.*;
import com.jeesite.modules.st.entity.formula.StFormulaNameChild;
import com.jeesite.modules.st.pojo.AjaxJson;
import com.jeesite.modules.st.service.StMaterialRecordService;
import com.jeesite.modules.st.service.StProductRecordService;
import com.jeesite.modules.st.service.formula.StFormulaNameService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.st.service.StProductBillService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 生产工单Controller
 *
 * @author HOHD
 * @version 2020-12-19
 */
@Controller
@RequestMapping(value = "${adminPath}/st/stProductBill")
public class StProductBillController extends BaseController {

    @Autowired
    private StProductBillService stProductBillService;

    /**
     * 获取数据
     */
    @ModelAttribute
    public StProductBill get(String id, boolean isNewRecord) {
        return stProductBillService.get(id, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("st:stProductBill:view")
    @RequestMapping(value = {"list", ""})
    public String list(StProductBill stProductBill, Model model) {
        model.addAttribute("stProductBill", stProductBill);
        return "modules/st/stProductBillList";
    }

    @RequiresPermissions("st:stProductBill:view")
    @RequestMapping(value = {"listSub"})
    public String listSub(StProductBill stProductBill, Model model) {
        model.addAttribute("stProductBill", stProductBill);
        return "modules/st/stProductBillList-sub";
    }

    @RequiresPermissions("st:stProductBill:view")
    @RequestMapping(value = {"listDetail"})
    public String listDetail(StProductBill stProductBill, Model model) {
        model.addAttribute("stProductBill", stProductBill);
        return "modules/st/stProductBillList-detail";
    }

    @RequiresPermissions("st:stProductBill:view")
    @RequestMapping(value = {"retrospect"})
    public String retrospect(StProductBillDetail stProductBillDetail, Model model) {
        model.addAttribute("stProductBill", stProductBillDetail);
        return "modules/st/stProductBillList-retrospect";
    }

    @RequiresPermissions("st:stProductBill:view")
    @RequestMapping(value = {"materialRetrospect"})
    public String materialRetrospect(StProductBillDetail stProductBillDetail, Model model) {
        model.addAttribute("stProductBill", stProductBillDetail);
        return "modules/st/stProductBillList-materialRetrospect";
    }
    @RequiresPermissions("st:stProductBill:view")
    @RequestMapping(value = {"productData"})
    public String productData(StProductBillDetail stProductBillDetail, Model model) {
        model.addAttribute("stProductBill", stProductBillDetail);
        return "modules/st/stProductBillList-productData";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("st:stProductBill:view")
    @RequestMapping(value = "listDetailData")
    @ResponseBody
    public Page<StProductBillBatchDetail> listDetailData(StProductBillBatchDetail stProductBillBatchDetail, HttpServletRequest request, HttpServletResponse response) {
        stProductBillBatchDetail.setPage(new Page<>(request, response));
        Page<StProductBillBatchDetail> page = stProductBillService.findDetail(stProductBillBatchDetail);
        return page;
    }

    @RequiresPermissions("st:stProductBill:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<StProductBill> listData(StProductBill stProductBill, HttpServletRequest request, HttpServletResponse response) {
        stProductBill.setPage(new Page<>(request, response));
        Page<StProductBill> page = stProductBillService.findPage(stProductBill);
        return page;
    }

    @RequiresPermissions("st:stProductBill:view")
    @RequestMapping(value = "listRetrospect")
    @ResponseBody
    public Page<StProductBillDetail> listRetrospect(StProductBillDetail stProductBillDetail, HttpServletRequest request, HttpServletResponse response) {
        stProductBillDetail.setPage(new Page<>(request, response));
        Page<StProductBillDetail> page = stProductBillService.findRetrospect(stProductBillDetail);
        return page;
    }

    @RequiresPermissions("st:stProductBill:view")
    @RequestMapping(value = "listMaterialRetrospect")
    @ResponseBody
    public Page<StProductBillBatchDetail> listMaterialRetrospect(StProductBillBatchDetail stProductBillBatchDetail, HttpServletRequest request, HttpServletResponse response) {
        stProductBillBatchDetail.setPage(new Page<>(request, response));
        Page<StProductBillBatchDetail> page = stProductBillService.materialRetrospect(stProductBillBatchDetail);
        return page;
    }
    @RequiresPermissions("st:stProductBill:view")
    @RequestMapping(value = "listProductData")
    @ResponseBody
    public Page<StWorkList> listProductData(StWorkList stWorkList, HttpServletRequest request, HttpServletResponse response) {
        stWorkList.setPage(new Page<>(request, response));
        Page<StWorkList> page = stProductBillService.productData(stWorkList);
        return page;
    }

//    @Autowired
//    private StProductBillDetailDao stProductBillDetailDao;

//    @RequiresPermissions("st:stProductBill:view")
//    @RequestMapping(value = "detail")
//    @ResponseBody
//    public AjaxJson<StProductBillDetail> detail(String id) {
//        AjaxJson<StProductBillDetail> ajaxJson = new AjaxJson<>();
//        StProductBillDetail detail = new StProductBillDetail();
//        StProductBill stProductBill = stProductBillService.get(id);
//        detail.setParentId(stProductBill);
//        detail.percentNe(-1);
//        List<StProductBillDetail> list = stProductBillDetailDao.findList(detail);
//        ajaxJson.extra = stProductBill;
//        ajaxJson.list  = list;
//        return ajaxJson;
//    }

    @Autowired
    StProductRecordService productRecordService;

    @Autowired
    StMaterialRecordService stMaterialRecordService;
    ObjectMapper objectMapper = new ObjectMapper();
    @Autowired
    private StFormulaNameService stFormulaNameService;

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("st:stProductBill:view")
    @RequestMapping(value = "form")
    public String form(StProductBill stProductBill, Model model) throws JsonProcessingException {
        List<StProductRecord> productRecordList = productRecordService.findList(new StProductRecord());
        model.addAttribute("productRecordList", productRecordList);

        Map<String, Object> map = new HashMap<>();
        List<StMaterialRecord> list = stMaterialRecordService.findAll(map);
        model.addAttribute("materialRecordList", objectMapper.writeValueAsString(list));
        map.put("code", "HJ");
        list = stMaterialRecordService.findAll(map);
        model.addAttribute("materialRecordListBean", list);

        map.clear();
        String thisFormula = stProductBill.getThisFormula();
        map.put("formulaNameId", thisFormula);
        List<StMaterialRecord> formulaMaterial = stMaterialRecordService.findAll(map);
        model.addAttribute("formulaMaterial", formulaMaterial);

        StFormulaNameChild stFormulaNameChild = new StFormulaNameChild();
        String productName = stProductBill.getProductName();
        stFormulaNameChild.setProductId(productName);
        List<StFormulaNameChild> listFormulaName;
        if (null != productName) {
            listFormulaName = stFormulaNameService.findList(stFormulaNameChild);
        } else {
            listFormulaName = new ArrayList<>();
        }

        model.addAttribute("listFormulaName", listFormulaName);
        model.addAttribute("stProductBill", stProductBill);
        return "modules/st/stProductBillForm";
    }

    @RequiresPermissions("st:stProductBill:view")
    @RequestMapping(value = "formulaMaterial")
    @ResponseBody
    public AjaxJson formulaMaterial(String formulaNameId) throws JsonProcessingException {
        AjaxJson<StMaterialRecord> ajaxJson = new AjaxJson<>();
        Map<String, Object> map = new HashMap<>();
        map.put("formulaNameId", formulaNameId);
        List<StMaterialRecord> list = stMaterialRecordService.findAll(map);
        ajaxJson.list = list;
        return ajaxJson;
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("st:stProductBill:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated StProductBill stProductBill) {
        stProductBillService.save(stProductBill);
        stProductBill.setStProductBillDetailList(null);
        stProductBill.setStProductBillBatchDetailList(null);
        return renderResult(Global.TRUE, text("保存生产工单成功！"), stProductBill);
    }

    /**
     * 删除数据
     */
    @RequiresPermissions("st:stProductBill:edit")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(StProductBill stProductBill) {
        stProductBillService.delete(stProductBill);
        return renderResult(Global.TRUE, text("删除生产工单成功！"));
    }

}